<template>
  <div class="all-courses-container">
    <div class="section-header">
      <h2 class="section-title">全部课程</h2>
      <el-button type="primary" text @click="router.push('/courses')">
        查看全部 <el-icon><ArrowRight /></el-icon>
      </el-button>
    </div>
    
    <div class="filter-bar">
      <div class="filter-item">
        <span class="filter-label">学科:</span>
        <div class="filter-options">
          <span 
            v-for="(subject, index) in subjects" 
            :key="index"
            :class="['filter-option', { active: activeSubject === subject.value }]"
            @click="activeSubject = subject.value"
          >
            {{ subject.label }}
          </span>
        </div>
      </div>
      
      <div class="filter-item">
        <span class="filter-label">难度:</span>
        <div class="filter-options">
          <span 
            v-for="(level, index) in levels" 
            :key="index"
            :class="['filter-option', { active: activeLevel === level.value }]"
            @click="activeLevel = level.value"
          >
            {{ level.label }}
          </span>
        </div>
      </div>
    </div>
    
    <div class="courses-grid">
      <div 
        v-for="(course, index) in filteredCourses" 
        :key="index" 
        class="course-card"
        @click="navigateToCourse(course)"
        :style="{ animationDelay: `${index * 0.1}s` }"
      >
        <div class="course-image" :style="{ backgroundImage: `url(${course.image})` }">
          <div class="course-level" :class="course.level">{{ getLevelText(course.level) }}</div>
          <div class="course-tag" v-if="course.tag">{{ course.tag }}</div>
        </div>
        <div class="course-info">
          <h3 class="course-title">{{ course.title }}</h3>
          <div class="course-meta">
            <div class="course-teacher" v-if="course.teacher">
              <el-avatar :size="24" :src="course.teacher.avatar"></el-avatar>
              <span>{{ course.teacher.name }}</span>
            </div>
            <div class="course-stats">
              <span><el-icon><User /></el-icon> {{ course.students }}</span>
              <span><el-icon><Star /></el-icon> {{ course.rating }}</span>
            </div>
          </div>
          <div class="course-bottom">
            <div class="course-price" :class="{ free: course.price === '免费' }">
              {{ course.price }}
            </div>
            <el-button size="small" type="primary">立即学习</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useStore } from 'vuex';
import { useRouter } from 'vue-router';
import { User, Star, ArrowRight } from '@element-plus/icons-vue';

const store = useStore();
const router = useRouter();
const activeSubject = ref('all');
const activeLevel = ref('all');

// 学科选项
const subjects = [
  { value: 'all', label: '全部' },
  { value: 'math', label: '数学' },
  { value: 'physics', label: '物理' },
  { value: 'chemistry', label: '化学' },
  { value: 'biology', label: '生物' },
  { value: 'english', label: '英语' }
];

// 难度选项
const levels = [
  { value: 'all', label: '全部' },
  { value: 'beginner', label: '初级' },
  { value: 'intermediate', label: '中级' },
  { value: 'advanced', label: '高级' }
];

// 从 store 获取课程数据
const allCourses = computed(() => store.state.home.allCourses || []);

// 根据筛选条件过滤课程
const filteredCourses = computed(() => {
  return allCourses.value.filter(course => {
    const subjectMatch = activeSubject.value === 'all' || course.subject === activeSubject.value;
    const levelMatch = activeLevel.value === 'all' || course.level === activeLevel.value;
    return subjectMatch && levelMatch;
  }).slice(0, 8); // 只显示前8门课程
});

// 获取难度文本
const getLevelText = (level) => {
  switch (level) {
    case 'beginner': return '初级';
    case 'intermediate': return '中级';
    case 'advanced': return '高级';
    default: return '所有级别';
  }
};

// 导航到课程详情页
const navigateToCourse = (course) => {
  router.push(`/course/${course.id}`);
};
</script>

<style lang="scss" scoped>
.all-courses-container {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  margin: 20px 0;
  
  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    border-bottom: 1px solid #ebeef5;
    
    .section-title {
      margin: 0;
      font-size: 18px;
      color: #303133;
    }
  }
  
  .filter-bar {
    padding: 15px;
    border-bottom: 1px solid #ebeef5;
    
    .filter-item {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .filter-label {
        flex: 0 0 50px;
        color: #606266;
      }
      
      .filter-options {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        
        .filter-option {
          padding: 5px 15px;
          margin-right: 10px;
          margin-bottom: 5px;
          border-radius: 4px;
          cursor: pointer;
          transition: all 0.3s;
          
          &:hover {
            color: #1e88e5;
            background-color: #ecf5ff;
          }
          
          &.active {
            color: #1e88e5;
            background-color: #ecf5ff;
            font-weight: bold;
          }
        }
      }
    }
  }
  
  .courses-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    padding: 20px;
    
    @media (max-width: 1200px) {
      grid-template-columns: repeat(3, 1fr);
    }
    
    @media (max-width: 768px) {
      grid-template-columns: repeat(2, 1fr);
    }
    
    .course-card {
      background-color: #fff;
      border-radius: 8px;
      overflow: hidden;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
      transition: transform 0.3s, box-shadow 0.3s;
      cursor: pointer;
      animation: fadeIn 0.5s ease-out forwards;
      opacity: 0;
      
      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
      }
      
      .course-image {
        height: 160px;
        background-size: cover;
        background-position: center;
        position: relative;
        
        .course-level {
          position: absolute;
          top: 10px;
          left: 10px;
          padding: 2px 8px;
          border-radius: 4px;
          font-size: 12px;
          background-color: rgba(0, 0, 0, 0.5);
          color: #fff;
          
          &.beginner {
            background-color: rgba(103, 194, 58, 0.8);
          }
          
          &.elementary {
            background-color: rgba(230, 162, 60, 0.8);
          }
          
          &.intermediate {
            background-color: rgba(30, 136, 229, 0.8);
          }
          
          &.advanced {
            background-color: rgba(245, 108, 108, 0.8);
          }
        }
        
        .course-tag {
          position: absolute;
          top: 10px;
          right: 10px;
          background-color: rgba(30, 136, 229, 0.8);
          color: #fff;
          padding: 2px 8px;
          border-radius: 4px;
          font-size: 12px;
        }
      }
      
      .course-info {
        padding: 15px;
        
        .course-title {
          margin: 0 0 10px;
          font-size: 16px;
          color: #303133;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        
        .course-meta {
          display: flex;
          justify-content: space-between;
          margin-bottom: 15px;
          
          .course-teacher {
            display: flex;
            align-items: center;
            
            span {
              margin-left: 10px;
              color: #606266;
              font-size: 14px;
            }
          }
          
          .course-stats {
            display: flex;
            
            span {
              display: flex;
              align-items: center;
              margin-left: 15px;
              color: #909399;
              font-size: 14px;
              
              .el-icon {
                margin-right: 5px;
              }
            }
          }
        }
        
        .course-bottom {
          display: flex;
          justify-content: space-between;
          align-items: center;
          
          .course-price {
            font-weight: bold;
            color: #f56c6c;
            
            &.free {
              color: #67c23a;
            }
          }
        }
      }
    }
  }
}
</style> 